-- Arrays can be used to represent vectors. Addition, subtraction and multiplication by a scalar can be done directly. The following functions implement other basic vector operations.

dot(A,B) = sum(A[i]*B[i],i,1,count(A))

magnitude(A) = sqrt(dot(A,A))

cross(A,B) = {A[2]*B[3]-A[3]*B[2],  -- 3D only
              A[3]*B[1]-A[1]*B[3],
              A[1]*B[2]-A[2]*B[1]}

-- cartesian to spherical coordinates
spherical(A) = {magnitude(A),
                acos(A[3]/magnitude(A)),
                atan2(A[2],A[1])}

-- spherical to cartesian coordinates
cartesian(r,theta,phi) = {r*sin(theta)*cos(phi),
                          r*sin(theta)*sin(phi),
                          r*cos(theta) }
